﻿using System.Collections;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace SinglePanelManage_
{
    /// <summary>
    /// UI页面父类,会自动绑定事件  按钮名称第一个 _ 以后为方法名
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class PanelBase<T> : PanelBaseBase where T : PanelBase<T>
    {
        public static T instance;//T 是子类类型 (具体的页面类型)

        protected virtual  void Start()
        {
            
        }

        public override  void Init()
        {
            instance = this as T;
            Debug.Log("init:"+typeof(T));
            //拿到所有按钮绑定事件
            var buttons=  GetComponentsInChildren<Button>();
            foreach (var item in buttons)
            {
                Button btn = item;
                string functionName = btn.name.Substring(btn.name.IndexOf('_')+1);
                btn.onClick.AddListener(()=> {
                    this.SendMessage(functionName);
                });
            }



        }
        public virtual void Open()
        {
            this.gameObject.SetActive(true);
            this.transform.SetAsLastSibling();
        }
        public virtual void Close()
        {
            this.gameObject.SetActive(false);
        }

        //方便给动态增加的按钮添加事件
        public static void AddButtonEvent(GameObject go, UnityAction action) 
        {
            go.GetComponent<Button>().onClick.AddListener(action);
        }
    }
}




///期望使用方式:
///拿页面   PanelMain.Instance.Open();
